<?php
namespace app\admin\service;
use auth\Action;
use app\admin\model\SystemNav;
///
//用于定义用户相关的服务接口等
//使用  Model('nav','Service')->getNavTpl();
//
class Nav  {
		
	//<!--  = 获取导航模板  =  -->
	public function getNavTpl($userInfo,$thisUrl){
		$action = Action::instance($userInfo);
		$app_path= config('template.tpl_replace_string.__ROOT_INDEX__');
		$navs = (new SystemNav)->getNavData();
		$is_open_nav =  getSystem('base_is_open_nav');

		$headerNavIndex = cookie('headerNavIndex');

		$htmls=array();
		foreach($navs as $key => $value){
		    
		    if($value['nav_status'] == 1 || $value['nav_pid'] !== 0 || !$action->check( $value['func_code'] ,'00') ){
		       continue;
		    }
            
            $html = '';
            $purl = $value['nav_url'];
            $icon = htmlspecialchars_decode($value['icon']);
            if($purl){
                //一级
                $itemClass = $this->isSelesd($purl,$thisUrl);
                $html.="
                    <li class='layui-nav-item $itemClass add'>
                        <a href='{$app_path}/$purl'>
                            <i class='iconfont'>$icon</i> $value[nav_name]
                        </a>
                    </li>    
                ";
            }else{
                //二级
                $itemClass = '';
                $dds = '';
                foreach($navs as $v){
                    if($this->isContinue($value,$v,$action)) continue;
                    $itemClass = $itemClass ?: $this->isSelesd($v['nav_url'],$thisUrl);
                    if( $is_open_nav == 1 && $value['nav_name'] != '系统设置'){
                        $itemClass = 'layui-nav-itemed';
                    }
                    $ic = htmlspecialchars_decode($v['icon']);
                    $class = $this->isSelesd($v['nav_url'],$thisUrl,'layui-this');
                    
                    //三级
                    $itemClass3 = '';
                    $dds3 = '';
                    foreach($navs as $sv){
                        if($this->isContinue($v,$sv,$action)) continue;
                        $itemClass3 = $itemClass3 ?: $this->isSelesd($sv['nav_url'],$thisUrl);
                        if($itemClass3){ $itemClass = $itemClass3; }
                        $ic3 = htmlspecialchars_decode($sv['icon']);
                        $class3 = $this->isSelesd($sv['nav_url'],$thisUrl,'layui-this');
                        $dds3.="<dd class='$class3' >";
                        $dds3.="
                            <a href='{$app_path}/$sv[nav_url]'>
                                <i class='iconfont'>$ic3</i> $sv[nav_name]
                            </a>
                        ";
                        $dds3.="</dd>";
                    }
                    
                    if($dds3)  {
                        $dds.= "<li class='layui-nav-item $itemClass3'>";
                        if( $v['nav_url'] ) $dds.= "<a href='{$app_path}/$v[nav_url]'>";
                        else $dds.= "<a>";
                        $dds.= "
                                <i class='iconfont'>$ic</i> $v[nav_name]</a>
                                <dl class='layui-nav-child'>
                                    $dds3
                                </dl>
                        ";
                    }else{
//                      $dds.="
//                          <dd class='$class'>
//                              <a href='{$app_path}/$v[nav_url]'>
//                                  <i class='iconfont'>$ic</i> $v[nav_name]
//                              </a>   
//                          </dd> 
//                      ";
                        $dds.="
                            <li class='$class layui-nav-item'>
                                <a href='{$app_path}/$v[nav_url]'>
                                    <i class='iconfont'>$ic</i> $v[nav_name]
                                </a>   
                            </li> 
                        ";
                    }
                };
                
                if(!empty($dds)){
                	$html.="$dds";
//                  $html.="
//                      <li class='layui-nav-item $itemClass'>
//                          <a><i class='iconfont'>$icon</i> {$value["nav_name"]}</a>
//                          <dl class='layui-nav-child'>$dds </dl>    
//                      </li>
//                  ";
                }
            }
            array_push($htmls,$html);
		}
		
		
		$headerNavIndex = cookie('headerNavIndex') ?: 0;
		return $htmls[$headerNavIndex];
		//return join(' ', $htmls);
	}
    
    private function isSelesd($navUrl,$thisUrl,$class='layui-nav-itemed'){
       return clear_urlcan(str_replace('index.php/','',$navUrl)) == $thisUrl ? $class : '';
    }
    
    private function isContinue($fv,$v,$action){
        if($v['nav_pid'] != $fv['nav_id'] || $v['nav_status'] == 1 || !$action->check($v['func_code'] ,'00')){
            return true;
        }
        return false;
    }
	
	//<!--  = 获取最后备份时间  =  -->
	public function getLastBackupsDate($format){
		$dir =  'backup/pageMng';
		if(!is_dir($dir)){ 
			return -1;
		};
		$dir = iconv('UTF-8','GBK',$dir);
		$_file=iconv('GBK','UTF-8',substr($dir,strripos($dir,"\\")+1));
		$handle = opendir($dir);
		$files = array();
		$files2 = array();
		while (($fl = readdir($handle)) !== false) {
			$temp = $dir.DIRECTORY_SEPARATOR.$fl;//转换成utf-8格式
			//是否文件侠
			if (is_dir($temp) && $fl != '.' && $fl != '..') {
				$this->read_all($temp,2,$mtime);
			} else {
				if ($fl != '.' && $fl != '..') {
					$stat = stat($temp);
					$files[]  = substr($fl,0,10);
					$files2[]= date($format, $stat['mtime']);
				}
			}
		}
		if($format=="Y-m-d H:i:s"){
			if(max($files2)){
				return max($files2);
			}else{
				return -2;
			}
		}else{
			return max($files);
		}	
	}
	
}